<?php

/**
 * Created by PhpStorm.
 * User: GadflyBSD
 * Date: 16/4/25
 * Time: 17:09
 */
class AppAction extends CommonAction{
	public function _initialize(){
		$this->defaultHeader($_SERVER['REQUEST_METHOD']);
	}

	/*public function ajax(){
		if(IS_AJAX && IS_POST && I('post.')){
			$module = I('post.ac').'Ajax';
			if(method_exists($this, $module))
				$return = call_user_func(array(&$this, $module));
			else
				$return = array('type' => 'error', 'content' => '系统没有找到'.$module.'方法', 'title' => '出错啦');
			if(is_array($return))
				$this->ajaxReturn($return);
			else
				echo $return;
		}
	}*/

	/*public function angular(){
		$request = $_SERVER['REQUEST_METHOD'];
		switch($request){
			case 'POST':
				$post = json_decode(file_get_contents('php://input'),true);
				$module = $post['ac'].'Ajax';
				if(method_exists($this, $post['ac']))
					$return = call_user_func(array(&$this, $module), $post);
				else
					$return = array('type' => 'error', 'content' => '系统没有找到'.$module.'方法', 'title' => '出错啦');
				dump($post);
				break;
			case 'GET':
				$get = I('get.');
				if(method_exists($this, $get['ac']))
					$return = call_user_func(array(&$this, $get['ac']), $get);
				else
					$return = array('type' => 'error', 'content' => '系统没有找到'.$get['ac'].'方法', 'title' => '出错啦');
				break;
		}
		if(is_array($return))
			$this->ajax($return);
		else
			echo $return;
	}*/

	/**
	 * 生成并在页面输出显示二维码
	 * @param $url
	 */
	public function qrcode(){
		vendor("phpqrcode.phpqrcode");
		QRcode::png(urldecode(I('get.s')), false, 'L', 10, 1);
	}

	public function test(){
		dump(M('WakaBindView')->where('role="survey"')->field('nickname, schoolname, survey_id')->select());
	}

	/**
	 * angularJS 中AJAX的POST提交方法的处理控制器
	 * @param $post
	 *
	 * @return mixed
	 */
	protected function angularAjax($post){
		switch($post['op']){
			case 'sendSms':
				return D('App')->sendSms($post);
			case 'register':
				return D('App')->register($post);
			case 'login':
				return D('App')->login($post);
			case 'logout':
				return D('App')->logout();
			case 'setRealname':
				return D('App')->setRealname($post);
			case 'getSession':
				return D('Public')->getSession($post['refresh']);
		}
	}

	protected function angularGet($get){
		switch($get['op']){
			case 'getSession':
				$refresh = is_set($get['refresh'])?$get['refresh']:false;
				return D('Public')->getSession($refresh);
			case 'getSurvey':
				return D('App')->getSurvey($get['schoolname']);
		}
	}

	protected function validationPost($post){
		switch($post['op']){
			case 'sms':
				Vendor('CtcSms');
				$sms = new CtcSms();
				return array('isUnique' =>$sms->verify(array('mobile' => $post['mobile'], 'code' => $post['mbcode'])));
		}
	}

	protected function defaultHeader($request='ALL'){
		header("Content-type: json; charset=utf-8");
		header('Access-Control-Allow-Origin:*');
		header('Access-Control-Allow-Headers:x-requested-with,content-type');
		switch($request){
			case 'GET':
				header('Access-Control-Allow-Methods:GET');
				return $this->isGet();
			case 'POST':
				header('Access-Control-Allow-Methods:POST');
				return $this->isPost();
			case 'PUT':
				header('Access-Control-Allow-Methods:PUT');
				return $this->isPut();
			case 'DELECT':
				header('Access-Control-Allow-Methods:DELETE');
				return $this->isDelete();
			case 'ALL':
				header('Access-Control-Allow-Methods:POST, GET, PUT, DELETE');
				return ($this->isGet() || $this->isPost() || $this->isPut() || $this->isDelete())?true:false;
			default:
				return false;
		}
	}
}